package com.yunzai.secure.domain;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serial;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class LoginUser implements Serializable, UserDetails {

    @Serial
    private static final long serialVersionUID = 1L;

    private Long id;

    private String username;

    private String password;

    private String nickname;

    private String sex;

    private String phone;

    private String email;

    private String avatar;

    private List<LoginRole> roles;

    private List<LoginPermission> permissions;

    private List<GrantedAuthority> authorities;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getAvatar() {
        return avatar;
    }

    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }

    public List<LoginRole> getRoles() {
        return roles;
    }

    public void setRoles(List<LoginRole> roles) {
        this.roles = roles;
    }

    public List<LoginPermission> getPermissions() {
        return permissions;
    }

    public void setPermissions(List<LoginPermission> permissions) {
        this.permissions = permissions;
    }

    /**
     * @return
     */
    @Override
    public boolean isAccountNonExpired() {
        return UserDetails.super.isAccountNonExpired();
    }

    /**
     * @return
     */
    @Override
    public boolean isAccountNonLocked() {
        return UserDetails.super.isAccountNonLocked();
    }

    /**
     * @return
     */
    @Override
    public boolean isCredentialsNonExpired() {
        return UserDetails.super.isCredentialsNonExpired();
    }

    /**
     * @return
     */
    @Override
    public boolean isEnabled() {
        return UserDetails.super.isEnabled();
    }

    /**
     * @return
     */
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        if (roles != null) {
            if(authorities == null) authorities = new ArrayList<>();
            roles.forEach(r -> {
                List<GrantedAuthority> list = authorities.stream().filter(a -> a.getAuthority().equals("ROLE_" + r.getKey())).toList();
                if(list.isEmpty()) authorities.add(new SimpleGrantedAuthority("ROLE_" + r.getKey()));
            });
        }
        if(permissions != null) {
            if(authorities == null) authorities = new ArrayList<>();
            permissions.forEach(r -> {
                List<GrantedAuthority> list = authorities.stream().filter(a -> a.getAuthority().equals(r.getKey())).toList();
                if(list.isEmpty()) authorities.add(new SimpleGrantedAuthority(r.getKey()));
            });
        }
        return authorities;
    }
}
